import java.awt.Color;
import java.util.concurrent.Semaphore;

import javax.swing.*;

public class Balls {
	
	
    public static void nap(int ms) {
	try {
	    Thread.sleep(ms);
	}
	catch(InterruptedException e) {
	    //
	    //  Print out the name of the tread that caused this.
	    //
	    System.err.println("Thread "+Thread.currentThread().getName()+
			       " throwed exception "+e.getMessage());
	}
    }

    public static void main(String[] a) {
	
    	final BallWorld world = new BallWorld();
    	final JFrame win = new JFrame();
    	SwingUtilities.invokeLater(new Runnable() {
    		public void run() {
    				win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					win.getContentPane().add(world);
					win.pack();
					win.setVisible(true);
    		}});
	
    	Thread.currentThread().setName("MyMainThread");
    	
    	Semaphore terminate = new Semaphore(0);
    	
		nap((int)(5000*Math.random()));
		new Ball("red", terminate, world, 50, 80, 5, 10, Color.red).start();
		nap((int)(5000*Math.random()));
		new Ball("blue",terminate, world, 70, 100, 8, 6, Color.blue).start();
		nap((int)(5000*Math.random()));
		new Ball("green", terminate, world, 150, 100, 9, 7, Color.green).start();
		nap((int)(5000*Math.random()));
		new Ball("black", terminate, world, 200, 130, 3, 8, Color.black).start();
		nap((int)(5000*Math.random()));
		
//		for(int i=0; i < 4; i++){
//			nap((int)(1000*Math.random()));
//			terminate.release();
//		}
		
		terminate.release();
		
		System.out.println(terminate.toString());
    }
}
